package com.emm.yixun.common.utils;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

public class MsgUtil {
	public static final String ROOM_ID="roomId";
	public static final String ROOM_NAME="roomName";
	public static final String CUSTOMER_ID="customerId";
	public static final String CUSTOMER_NAME="customerName";
	public static final String PRICE_SCHEME_ID="priceSchemeId";
	public static final String PRICE_SCHEME_NAME="priceSchemeName";
	public static final String NEXT_NODE_ID="nextNodeId";
	public static final String PROCESS_INSTANCE_ID="processInstanceId";
	public static final String BUSINESS_ID="businessId";
	
    public static String getReplaceValues(Map<String,String> map){
    	JSONObject object=new JSONObject();
    	for(String key : map.keySet() ){
    		object.put(key, map.get(key));
    	}
    	return object.toJSONString();
    }
	//消息内容添加链接
    public static String addLink(String content,String replaceValues){
    	if(StringUtils.isBlank(replaceValues))return content;
    	JSONObject object=JSON.parseObject(replaceValues);
    	String roomId=object.getString(MsgUtil.ROOM_ID);
    	String roomName=object.getString(MsgUtil.ROOM_NAME);
    	String customerId=object.getString(MsgUtil.CUSTOMER_ID);
    	String customerName=object.getString(MsgUtil.CUSTOMER_NAME);
    	String priceSchemeId=object.getString(MsgUtil.PRICE_SCHEME_ID);
    	String priceSchemeName=object.getString(MsgUtil.PRICE_SCHEME_NAME);
    	if(StringUtils.isNotBlank(roomId) && StringUtils.isNotBlank(roomName)){
    		String addClickReplace=addClick(roomName,getFunStr(roomId,"1"));
    		content=content.replaceAll(roomName, addClickReplace);
    	}
    	if(StringUtils.isNotBlank(customerId) && StringUtils.isNotBlank(customerName)){
    		String addClickReplace=addClick(customerName,getFunStr(customerId,"2"));
    		content=content.replaceAll(customerName, addClickReplace);
    	}
    	if(StringUtils.isNotBlank(priceSchemeId) && StringUtils.isNotBlank(priceSchemeName)){
    		String addClickReplace=addClick(priceSchemeName,getFunStr(priceSchemeId,"3"));
    		content=content.replaceAll(priceSchemeName, addClickReplace);
    	}
    	return content;
    }
    

    
    private static String getFunStr(String id,String type){
    	return "messageJump('"+id+"','"+type+"')";
    }
    
    
    private static String addClick(String name,String funStr){
    	return "<a style=\"color:blue;text-decoration:underline;\" onclick=\""+funStr+"\">"+name+"</a>";
    }
    
    public static void main(String[] args) {
    	String content="更名审批申请、房号：新房-->二期-->A-->17；客户：韩贞荣；经纪人：张卫玲";
    	String roomName="新房-->二期-->A-->17";
    	String customerName="韩贞荣";
    	Map<String,String> object=new HashMap<String,String>();
    	object.put(MsgUtil.ROOM_ID, "1");
    	object.put(MsgUtil.ROOM_NAME, roomName);
    	object.put(MsgUtil.CUSTOMER_ID, "2");
    	object.put(MsgUtil.CUSTOMER_NAME, customerName);
    	object.put(MsgUtil.PRICE_SCHEME_ID, "3");
    	object.put(MsgUtil.PRICE_SCHEME_NAME, "价格方案");
    	object.put(MsgUtil.BUSINESS_ID, "1234");
    	String replaceValues=getReplaceValues(object);
    	System.out.println(replaceValues);
    	//存入数据库
    	//从数据库获取，处理显示
		System.out.println(addLink(content,replaceValues));
		
	}
    
    
    
    
}
